﻿package com.gengine.modules
{
    import com.gengine.core.*;
    import com.gengine.debug.*;
    import com.gengine.utils.pools.*;
    import flash.events.*;
    import flash.system.*;
    import flash.utils.*;

    public class ModuleManager extends Singleton
    {
        private var _modulesMap:Dictionary;
        private var _moduleList:Array;
        private var _tempModuleInfo:ModuleInfo;
        private var _isloading:Boolean = false;
        private static var _instance:ModuleManager;

        public function ModuleManager()
        {
            this._modulesMap = new Dictionary();
            this._moduleList = new Array();
            return;
        }// end function

        public function loadModuleInfo(param1:Array) : void
        {
            var _loc_2:ModuleInfo = null;
            var _loc_3:int = 0;
            while (_loc_3 < param1.length)
            {
                
                _loc_2 = param1[_loc_3] as ModuleInfo;
                this._modulesMap[_loc_2.url] = _loc_2;
                _loc_3++;
            }
            return;
        }// end function

        private function getModuleInfo(param1:String) : ModuleInfo
        {
            return this._modulesMap[param1] as ModuleInfo;
        }// end function

        public function loadModule(param1:String) : ModuleInfo
        {
            var _loc_2:* = this.getModuleInfo(param1) as ModuleInfo;
            if (_loc_2)
            {
                if (_loc_2.isLoaded)
                {
                    this.loaderComplete(_loc_2);
                }
                else if (_loc_2.isLoading)
                {
                }
                else
                {
                    this._moduleList.push(_loc_2);
                    _loc_2.isLoading = true;
                    if (this._isloading == false)
                    {
                        this.loadNextModule();
                    }
                }
                return _loc_2;
            }
            else
            {
            }
            throw new Error("没有该模块信息,请调用loadModuleInfo url=" + param1);
        }// end function

        private function loadNextModule(param1:LoaderContext = null) : void
        {
            var _loc_2:* = this._moduleList.shift() as ModuleInfo;
            if (_loc_2)
            {
                this._isloading = true;
                this.loadModuleImpl(_loc_2, param1);
            }
            else
            {
                this._isloading = false;
            }
            return;
        }// end function

        private function loadModuleImpl(param1:ModuleInfo, param2:LoaderContext = null) : void
        {
            var _loc_3:* = ObjectPool.getObject(ModuleLoader);
            _loc_3.addEventListener(Event.COMPLETE, this.onCompleteHandler);
            _loc_3.addEventListener(ProgressEvent.PROGRESS, this.onProgressHandler);
            _loc_3.addEventListener(IOErrorEvent.IO_ERROR, this.onIoErrorHandler);
            _loc_3.load(param1.url, param2);
            return;
        }// end function

        private function loaderComplete(param1:ModuleInfo) : void
        {
            param1.dispatchEvent(new Event(Event.COMPLETE));
            return;
        }// end function

        private function onCompleteHandler(event:Event) : void
        {
            var _loc_2:* = event.target as ModuleLoader;
            _loc_2.removeEventListener(Event.COMPLETE, this.onCompleteHandler);
            _loc_2.removeEventListener(ProgressEvent.PROGRESS, this.onProgressHandler);
            _loc_2.removeEventListener(IOErrorEvent.IO_ERROR, this.onIoErrorHandler);
            var _loc_3:* = this.getModuleInfo(_loc_2.url);
            _loc_3.isLoaded = true;
            _loc_3.isLoading = false;
            if (_loc_2.content is IModule)
            {
                _loc_3.module = _loc_2.content as IModule;
            }
            else
            {
                throw new Error("加载的不是模块，请实现IModule 接口");
            }
            this.loaderComplete(_loc_3);
            this.loadNextModule();
            this._tempModuleInfo = null;
            return;
        }// end function

        private function onProgressHandler(event:ProgressEvent) : void
        {
            var _loc_2:* = event.target as ModuleLoader;
            var _loc_3:* = this.getModuleInfo(_loc_2.url);
            if (_loc_3)
            {
                _loc_3.dispatchEvent(event);
            }
            return;
        }// end function

        private function onIoErrorHandler(event:IOErrorEvent) : void
        {
            var _loc_2:* = event.target as ModuleLoader;
            var _loc_3:* = this.getModuleInfo(_loc_2.url);
            if (_loc_3)
            {
                _loc_3.dispatchEvent(event);
                _loc_3.error = true;
                _loc_3.isLoaded = false;
                _loc_3.isLoading = false;
            }
            this.loadNextModule();
            Log.system("ModuleManager:" + event.text);
            return;
        }// end function

        public static function get instance() : ModuleManager
        {
            if (_instance == null)
            {
                _instance = new ModuleManager;
            }
            return _instance;
        }// end function

    }
}
